package icu.ac.kr.simpleRssReader.activity;

import icu.ac.kr.simpleRssReader.provider.RssDBProviderConstant;
import icu.ac.kr.simpleRssReader.view.ChannelHead;
import icu.ac.kr.simpleRssReader.view.PostRow;
 
import icu.ac.kr.simpleRssReader.*;
import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class PostList extends ListActivity {

	static final String TAG = "PostList";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	
		setContentView(R.layout.postlist);
		
		Intent intent = getIntent();
		Uri uri = null;
		if (intent != null)
			uri = intent.getData();	
	
		ChannelHead header = (ChannelHead)findViewById(R.id.chHeader);
		header.bindData(uri.getPathSegments().get(1));
		Cursor cursor = getContentResolver().query(uri,new String[] {"_id", "title", "pubdate","read", "body", "channel_id"}, "channel_id = '"+uri.getPathSegments().get(1)+"'", null, null);
		
		while(cursor.moveToNext())
		{
		Log.d("TAG",cursor.getString(cursor.getColumnIndex(RssDBProviderConstant.post.PUBDATE))+"---"+cursor.getString(cursor.getColumnIndex(RssDBProviderConstant.post.BODY)));	
			
		}
		
		PostCursor postCursor = new PostCursor(this, cursor);
		setListAdapter(postCursor);
		
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
	
		startActivity(new Intent(Intent.ACTION_VIEW, ContentUris.withAppendedId(RssDBProviderConstant.POST_URI, id )));
		finish();
		
		
	}

	private class PostCursor extends CursorAdapter{

		public PostCursor(Context context, Cursor c) {
			super(context, c);
			// TODO Auto-generated constructor stub
		}
		

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			// TODO Auto-generated method stub
			((PostRow)view).bindData(cursor);
		
		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			// TODO Auto-generated method stub
			View postRow = new PostRow(PostList.this, null);
			return postRow;
		}
		
		
		
		
	}
	
	
	
}
